package com.learn.sb.webfluxdemo.handler;

import com.learn.sb.webfluxdemo.eitity.User;
import com.learn.sb.webfluxdemo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class UserHandler {

	@Autowired
	UserService userService;

	public Mono<ServerResponse> getUserList(ServerRequest request) {
		Flux<User> userFlux = userService.findUserList();
//		userFlux.subscribe(user -> log.info(user.toString()));

		return ServerResponse.ok().body(userFlux, User.class);
	}

	public Mono<ServerResponse> getUser(ServerRequest request) {
		String userId = request.pathVariable("userId");
		Mono<User> userMono = userService.findUserById(userId);
//		userMono.subscribe(user -> log.info(user.toString()));

		return ServerResponse.ok().body(userMono, User.class);
	}
}
